/*
 * Copyright (c) 2016. zhaojin 15398699939@163.com
 */

package com.bankscene.screenfit.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.bankscene.screenfit.R;
import com.bankscene.screenfit.model.Menus;
import com.bankscene.screenfit.dict.ErrorDict;
import com.bankscene.screenfit.exception.PresenterModeException;
import com.bankscene.screenfit.util.ActivityUtils;
import com.bankscene.screenfit.vus.MenusVu;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.bankscene.screenfit.activity.MainActivity.MENU_ITEM;
import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Created by jiiiiiin on 6/14/16.
 */
public class MenusActivity extends AbstractMenusActivity<MenusVu> {

    public static final String LEFT_FRAGMENT = "leftFragment";
    private Logger log = LoggerFactory.getLogger(MenusActivity.class);
    private String leftFragmentClassName;

    @Override
    protected void onBindVu() {
        super.onBindVu();
        Bundle paramsBundle = getIntent().getExtras();
        Object leftFragmentTemp = paramsBundle.get(LEFT_FRAGMENT);
        checkNotNull(leftFragmentTemp, ErrorDict.ERROR_LEFTFRAGMENT_NOTBENULL);
        leftFragmentClassName = String.valueOf(leftFragmentTemp);
        Fragment rf = null;
        try {
            rf = (Fragment) Class.forName(chooseMenu.getClassName()).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        rf.setArguments(paramsBundle);
        ActivityUtils.addFragmentToActivity(fm, rf, R.id.right_layout);
    }

    @Override
    protected Class<MenusVu> getVuClass() {
        return MenusVu.class;
    }

    @Override
    public final void navigationItemSelectedDo(@NonNull Menus menus) {
        if (menus.equals(chooseMenu)) {
            Toast.makeText(MenusActivity.this, R.string.doagain, Toast.LENGTH_LONG).show();
        } else {
            chooseMenu = menus;
            setTitle(chooseMenu.getTitle());
            Bundle paramsBundle = new Bundle();
            paramsBundle.putParcelable(MENU_ITEM, chooseMenu);

            try {
                if (!TextUtils.isEmpty(chooseMenu.getClassName())) {
                    Fragment rf = (Fragment) Class.forName(chooseMenu.getClassName()).newInstance();
                    rf.setArguments(paramsBundle);
                    if (app.isPad()) {
                        ActivityUtils.replaceFragmentToActivityAndAddToBackStack(fm, rf, R.id.right_layout);
                    } else {
                        ActivityUtils.replaceFragmentToActivity(fm, rf, R.id.right_layout);
                    }
                } else if (chooseMenu.getFragments() != null && chooseMenu.getFragments().length > 0) {
                    paramsBundle.putString(LEFT_FRAGMENT, leftFragmentClassName);
                    MenusTabsActivity.newInstance(MenusActivity.this, paramsBundle);
                    finish();
                } else {
                    switch (menus.getId()) {
                        case R.id.logout_navigation_menu_item:
                            Snackbar.make(vu.getRootView(), getResources().getText(R.string.logout_q), Snackbar.LENGTH_LONG).setAction(getResources().getText(R.string.confirm), new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    ActivityUtils.logout(activityStackManager);
                                }
                            }).show();
                            break;
                        default:
                            throw new PresenterModeException(ErrorDict.ERROR_SELETEDMENUS_NOTBENULL);
                    }
                }

            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    public static void newInstance(@NonNull Context fromActCtx, @Nullable Bundle paramsBundle) {
        Intent intent = new Intent(fromActCtx, MenusActivity.class);
        if (null != paramsBundle) {
            intent.putExtras(paramsBundle);
        }
        fromActCtx.startActivity(intent);
    }


}
